﻿using System;
using System.IO;
using System.Threading;
using System.Web;
using System.Web.Caching;
using System.Windows.Forms;

namespace Universe.IoC.Tests.Samples
{
#if DEBUG
    internal class ReferenceUsages
    {
        private void Test()
        {
            {
                // Single Call
                // No Any Transient Lifetime Manager
                ConfigurationContainer container = new ConfigurationContainer();
                container.Register<ISampleInterface>(
                    delegate
                    {
                        return new SampleClass();
                    });
            }

            {
                // Singleton
                SampleClass instance = new SampleClass();
                ConfigurationContainer container = new ConfigurationContainer();
                container.Register<ISampleInterface>(
                    delegate
                    {
                        return instance;
                    });
            }

            {
                // REAL fluent injection
                ConfigurationContainer container = new ConfigurationContainer();
                container.Register<ISampleInterface>(
                    delegate
                    {
                        SampleClass ret = new SampleClass(12, "Bye-bye, Unity!");
                        ret.MyProperty = new object();
                        ret.MyStringProperty = "Some Text";
                        ret.InitializeMe(42.0m, container.Resolve<ILogger>());
                        return ret;
                    });

            }

            {
                // Per Thread Lifetime Manager
                ConfigurationContainer container = new ConfigurationContainer();
                container.Register<ISampleInterface>(
                    delegate
                    {
                        const string key = "Sample Interface Implementation";
                        LocalDataStoreSlot slot = Thread.GetNamedDataSlot(key);
                        SampleClass ret = (SampleClass) Thread.GetData(slot);
                        if (ret == null)
                        {
                            ret = new SampleClass();
                            Thread.SetData(slot, ret);
                        }

                        return ret;
                    });
            }

            {
                // Singleton living up to 60 minutes per instance
                ConfigurationContainer container = new ConfigurationContainer();
                container.Register<ISampleInterface>(
                    delegate
                    {
                        const string key = "Sample Interface Implementation";
                        SampleClass ret = (SampleClass) HttpRuntime.Cache.Get(key);
                        if (ret == null)
                        {
                            ret = new SampleClass();
                            HttpRuntime.Cache.Insert(
                                key,
                                ret,
                                null,
                                DateTime.Now.AddMinutes(60),
                                Cache.NoSlidingExpiration);
                        }

                        return ret;
                    });
            }



// Lazy Singleton
            {
                ConfigurationContainer container = new ConfigurationContainer();
                container.Register<ISampleInterface>(
                    delegate
                    {
                        Lazy<ISampleInterface> instance = new Lazy<ISampleInterface>(
                            () => new SampleClass(),
                            LazyThreadSafetyMode.ExecutionAndPublication);

                        return instance.Value;
                    });

            }


            {
                Control someControl = new Control();
                someControl.OnDisposingForm(
                    delegate
                    {
                        File.Delete("c:\\temp.xml");
                    });
            }

            {
                Stream stream = null;
                Control control = new Control();
                control.RegisterDisposable(stream);
            }


        }
    }
#endif
}
    

